/*
 * Powered By XY
 * Since 2020
 */
package com.xy.biz.fd.controller;

import com.xy.biz.fd.client.RepositoryClient;
import com.xy.biz.fd.domain.DocumentPermissions;
import com.xy.biz.fd.domain.converter.RepositoryConverter;
import com.xy.biz.fd.domain.query.RepositoryQuery;
import com.xy.biz.fd.domain.req.repository.GetRepositoryRequest;
import com.xy.biz.fd.domain.req.repository.GetTypeRepositoryRequest;
import com.xy.biz.fd.domain.req.repository.ListRepositoryRequest;
import com.xy.biz.fd.domain.req.repository.SaveRepositoryRequest;
import com.xy.biz.fd.domain.resp.GetRepositoryResponse;
import com.xy.biz.fd.domain.vo.TeamRepositoryTreeItem;
import com.xy.biz.fd.service.RepositoryService;
import com.xy.lang.domain.KeyValue;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.xy.biz.fd.domain.DocumentPermissions.*;

/**
 *
 *
 * @author ssl
 * @desc 知识库管理
 * @date 2021-1-18
 */
@Api(tags = "知识库")
@Slf4j
@RestController
@RequestMapping("/api/fd/repository")
public class RepositoryController implements RepositoryClient {

    @Autowired
    private RepositoryService repositoryService;

    /**
     * 新增知识库
     * @param request 知识库名称，知识库所属类型，知识库所属人(可为空，空为个人)
     * @return
     */
    @ApiOperation("新增知识库")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveRepository(@RequestBody SaveRepositoryRequest request) {
        return repositoryService.saveRepository(RepositoryConverter.toBO(request));
    }

    /**
     * 修改知识库名称或归属
     * @param request 知识库id,知识库新名称(判断当前知识库所属，判断新名称是否重复)
     * @return
     */
    @ApiOperation("修改知识库")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateRepository(@RequestBody SaveRepositoryRequest request) {
        return repositoryService.updateRepository(RepositoryConverter.toBO(request));
    }


    /**
     * 删除知识库
     * @param request 知识库id,判断知识库所属类型(0 判断知识库所属人是否为当前用户,1 判断当前知识库创建人是否为当前用户)
     * @return
     */
    @ApiOperation("删除知识库")
    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteRepository(@RequestBody GetRepositoryRequest request) {
        return repositoryService.deleteRepository(request.getRepositoryId());
    }

    @Deprecated
    @ApiOperation("通过id获取单个知识库")
    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetRepositoryResponse> getRepository(@RequestBody GetRepositoryRequest request) {
        GetRepositoryResponse getRepositoryResponse = RepositoryConverter.toResponse(repositoryService.getRepository(request.getRepositoryId()));
//        getRepositoryResponse.setPermissionResources(DocumentPermissions.getPermissionList());
        return Result.success(getRepositoryResponse);
    }

    @Deprecated
    @ApiOperation("通过id集合获取知识库集合")
    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetRepositoryResponse>> listRepositoryById(@RequestBody Collection<Long> collection) {
        return Result.success(RepositoryConverter.toResponse(repositoryService.listRepositoryById(collection)));
    }

    @ApiOperation("查询知识库")
    @PostMapping(value = "/list")
    @Override
    public Result<List<GetRepositoryResponse>> listRepository(@RequestBody ListRepositoryRequest request) {
        return Result.success(RepositoryConverter.toResponse(repositoryService.listRepository(RepositoryConverter.toQueryMax(request))));
    }

    @ApiOperation("知识库数量查询")
    @PostMapping(value = "/count")
    @Override
    public Result<Integer> countRepository(@RequestBody ListRepositoryRequest request) {
        return Result.success(repositoryService.countRepository(RepositoryConverter.toQuery(request)));
    }

    /**
     * 知识库查询
     * @param request pageIndex(起始下标),pageSize(条数),belongType(所属类型),belongTo(所属者，可为空)
     * @return
     */
    @ApiOperation("分页查询知识库")
    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetRepositoryResponse>> pageRepository(@RequestBody ListRepositoryRequest request) {
        RepositoryQuery query = RepositoryConverter.toQuery(request);
        request.setPageSize(1000);
        List<GetRepositoryResponse> list = RepositoryConverter.toResponse(repositoryService.listRepository(query));
        int count = repositoryService.countRepository(query);
        return Result.success(new Page<>(list, count));
    }


    @Override
    @ApiOperation("发行知识库")
    @PostMapping(value = "/get_release_repository")
    public Result<GetRepositoryResponse> getReleaseRepositoryInfo() {
        return repositoryService.getReleaseRepositoryInfo();
    }


    /**
     * 回收站获取
     * @param
     * @return
     */
    @ApiOperation("回收站")
    @PostMapping(value = "/get_rubbish")
    public Result<List<Object>> getRubbish() {
        return repositoryService.getRubbish();
    }


    /**
     * 回收站获取
     * @param
     * @return
     */
    @ApiOperation("台账知识库")
    @PostMapping(value = "/get_ledger")
    public Result<List<Object>> getLedger() {
        return repositoryService.getLedger();
    }


    /**
     * 用户部门知识库获取
     * @param
     * @return
     */
    @ApiOperation("用户部门知识库获取")
    @PostMapping(value = "/get_user_group_and_repository")
    public Result<?> getUserGroupAndRepository() {
        return repositoryService.getUserGroupAndRepository();
    }

    @ApiOperation("知识库团队成员的权限范围")
    @PostMapping(value = "/list_repository_member_permissions")
    public Result<List<KeyValue<Integer, String>>> listRepositoryMemberPermissions() {
        List<KeyValue<Integer, String>> permissionList = new ArrayList<>();
        permissionList.add(new KeyValue<>(PREVIEW, "预览"));
        permissionList.add(new KeyValue<>(PRINT, "打印"));
        permissionList.add(new KeyValue<>(DOWNLOAD, "下载"));
        permissionList.add(new KeyValue<>(RENAME, "重命名"));
        permissionList.add(new KeyValue<>(FAVORITE, "收藏"));
        permissionList.add(new KeyValue<>(MOVE, "移动"));
        permissionList.add(new KeyValue<>(COPY, "复制"));
        permissionList.add(new KeyValue<>(EDIT, "编辑"));
        permissionList.add(new KeyValue<>(NEW, "新增"));
        permissionList.add(new KeyValue<>(SHARE, "分享"));
        permissionList.add(new KeyValue<>(ASSOCIATE, "关联"));
        permissionList.add(new KeyValue<>(DELETE, "删除"));
        return Result.success(permissionList);
    }

    @ApiOperation("获取团队知识库的树")
    @Validatable
    @PostMapping(value = "/list_team_tree")
    public Result<List<TeamRepositoryTreeItem>> listUserTeamTree(@RequestBody GetTypeRepositoryRequest request) {
        return Result.success(repositoryService.getUserTeamListTree(request));
    }

}
